package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.core.entity.jz.BLogin;
import com.sxbbc.common.core.mapper.jz.BLoginMapper;
import com.sxbbc.common.core.service.IBLoginService;
import com.sxbbc.common.core.service.IBRolesService;
import com.sxbbc.common.core.utils.BasePage;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 店铺操作员表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-05-08
 */
@Service
public class BLoginServiceImpl extends ServiceImpl<BLoginMapper, BLogin> implements IBLoginService {
    @Autowired
    private IBRolesService bRolesService;

    @Override
    public BLogin selectByLogin(String login) {
        return super.getOne(new QueryWrapper<BLogin>().eq("login", login));
    }

    private QueryWrapper<BLogin> queryWrapper(BLogin bLogin) {
        QueryWrapper<BLogin> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(bLogin.getLogin())) {
            wrapper.eq("login", bLogin.getLogin());
        }
        if (StringUtils.isNotBlank(bLogin.getNickName())) {
            wrapper.eq("nickName", bLogin.getNickName());
        }
        if (bLogin.getBid() != null) {
            wrapper.eq("bid",bLogin.getBid());
        }
        return wrapper;
    }


    @Override
    public BasePage<BLogin> queryList(BasePage<BLogin> page, BLogin bLogin) {
        CalcuUtils calcuUtils = CalcuUtils.getInstance();
        QueryWrapper<BLogin> wrapper = queryWrapper(bLogin);
        if (page.isPaging()) {
            super.page(page, wrapper);
        } else {
            List<BLogin> list = super.list(wrapper);
            page.setRecords(list);
        }
        for (BLogin u : page.getRecords()) {
                u.setRoleName(bRolesService.getById(u.getRoleId()).getName());
        }
        return page;
    }

    @Override
    public boolean removeByBid(Integer bid) {
        QueryWrapper<BLogin> wrapper = new QueryWrapper<>();
        wrapper.eq("bid",bid);
        return super.remove(wrapper);
    }
}
